package com.kgcx.dataflow.task.forward.runner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TaskForwardCommandLineRunner implements CommandLineRunner {
    private static final Logger LOG = LoggerFactory.getLogger(TaskForwardCommandLineRunner.class);

    @Value("${kgcx.task.transmit.exchange:kgcxTaskExchange}")
    private String exchange;

    @Value("${kgcx.task.transmit.routing-key:kgcx.task.transmit}")
    private String routingKey;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.boot.CommandLineRunner#run(java.lang.String[])
     */
    @Override
    public void run(String... arg0) throws Exception {
        ConnectionFactory connectionFactory = rabbitTemplate.getConnectionFactory();
        LOG.info(String.format("转发MQ消息%s->%s到%s:%s?username=%s服务器。", exchange, routingKey, connectionFactory.getHost(),
                connectionFactory.getPort(), connectionFactory.getUsername()));

        rabbitTemplate.convertAndSend(exchange, routingKey, System.currentTimeMillis());
    }
}
